define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数
            Table.api.init({
                extend: {
                    index_url: 'daterestriction/index' + location.search,
                    add_url: 'daterestriction/add',
                    edit_url: 'daterestriction/edit',
                    del_url: 'daterestriction/del',
                    multi_url: 'daterestriction/multi',
                    import_url: 'daterestriction/import',
                    table: 'date_restriction',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'config_key', title: __('Config_key'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'description', title: __('Description'), operate: 'LIKE'},
                        {field: 'restriction_type', title: __('Restriction_type'), searchList: {"workdays":__('Restriction_type workdays'),"weekends":__('Restriction_type weekends'),"custom":__('Restriction_type custom'),"specific":__('Restriction_type specific'),"disabled":__('Restriction_type disabled')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                
                // 限制类型变化时显示/隐藏相关字段
                $(document).on('change', '#c-restriction_type', function() {
                    var type = $(this).val();
                    
                    // 隐藏所有配置组
                    $('#custom-weekdays-group, #specific-dates-group, #disabled-dates-group').hide();
                    
                    // 根据类型显示对应的配置组
                    switch(type) {
                        case 'custom':
                            $('#custom-weekdays-group').show();
                            break;
                        case 'specific':
                            $('#specific-dates-group').show();
                            break;
                        case 'disabled':
                            $('#disabled-dates-group').show();
                            break;
                    }
                });
                
                // 页面加载时触发一次变化事件
                $('#c-restriction_type').trigger('change');
                
                // 表单提交前处理数据
                $("form[role=form]").on('submit', function(e) {
                    var type = $('#c-restriction_type').val();
                    var configData = {};
                    
                    // 根据限制类型收集配置数据
                    switch(type) {
                        case 'custom':
                            var weekdays = [];
                            $('.weekday-checkbox:checked').each(function() {
                                weekdays.push($(this).val());
                            });
                            configData.allowed_weekdays = weekdays;
                            break;
                        case 'specific':
                            var dates = $('#c-allowed_dates').val().split('\n').filter(function(date) {
                                return date.trim() !== '';
                            });
                            configData.allowed_dates = dates;
                            break;
                        case 'disabled':
                            var dates = $('#c-disabled_dates').val().split('\n').filter(function(date) {
                                return date.trim() !== '';
                            });
                            configData.disabled_dates = dates;
                            break;
                    }
                    
                    // 将配置数据作为隐藏字段添加到表单
                    if (Object.keys(configData).length > 0) {
                        $('<input>').attr({
                            type: 'hidden',
                            name: 'row[config_data]',
                            value: JSON.stringify(configData)
                        }).appendTo(this);
                    }
                });
            }
        }
    };
    return Controller;
});